#include "Enemy.h"
#include "IsometricMap.h"
#include "Character.h"

Enemy::Enemy()
	:	mCharPos(0.0f, 0.0f)
	,	mIsClicked(false)
	,	mHealth(100)
	,	mMovement(1)
	,	mAttack(100)
	,	mDefence(0)
	,	mMagic(0)
	,	mCurrentState(E_topLeft)
	,	mIsMoving(false)
	,	mTempCharPos(0.0f, 0.0f)
	,	mVelocity(0.0f, 0.0f)
{
}
Enemy::~Enemy()
{}
void Enemy::StartMoving(SVector2 pos)
{
	mTempCharPos = mCharPos;
	mCharPos = pos;
	mVelocity = (mCharPos - mTempCharPos);
}
void Enemy::SetEnemyPos(SVector2 pos)
{
	mCharPos = pos;
	mTempCharPos = pos;
}
SVector2 Enemy::GetEnemyPos() const
{
	return mCharPos;
}
bool Enemy::IsClicked()
{
	if(mIsClicked == false)
	{
		mIsClicked = true;
	}
	else
	{
		mIsClicked = false;
	}
	return mIsClicked;
}
void Enemy::SetMoving(bool move)
{
	mIsMoving = move;
}

void Enemy::Load()
{
	mEnemySprite[E_topLeft].Load("prinny_2.png");
	mEnemySprite[E_topRight].Load("prinny_1.png");
	mEnemySprite[E_bottomLeft].Load("prinny_4.png");
	mEnemySprite[E_bottomRight].Load("prinny_3.png");

	mMovementLeftRight.Add("prinny_lr_1.png");
	mMovementLeftRight.Add("prinny_lr_2.png");
	mMovementLeftRight.Add("prinny_lr_3.png");
	mMovementLeftRight.Add("prinny_lr_4.png");
	mMovementLeftRight.Add("prinny_lr_5.png");
	mMovementLeftRight.Play(4.0f, true);
	
	mMovementRightLeft.Add("prinny_rl_1.png");
	mMovementRightLeft.Add("prinny_rl_2.png");
	mMovementRightLeft.Add("prinny_rl_3.png");
	mMovementRightLeft.Add("prinny_rl_4.png");
	mMovementRightLeft.Add("prinny_rl_5.png");
	mMovementRightLeft.Play(4.0f, true);
}
void Enemy::Unload()
{
	for(int i = 0; i < E_dead; ++i)
	{
		mEnemySprite[i].Unload();
	}
	mMovementRightLeft.Unload();
	mMovementLeftRight.Unload();
}
void Enemy::Update(float deltaTime)
{

	for(int i = E_topLeft; i < E_dead; ++i)
	{
		mEnemySprite[i].Update(deltaTime);
	}
	mMovementRightLeft.Update(deltaTime);
	mMovementLeftRight.Update(deltaTime);
	
	if(fabs(mTempCharPos.x - mCharPos.x) < 1.0f &&
		fabs(mTempCharPos.y - mCharPos.y) < 1.0f)
	{
		SetMoving(false);
	}
	else if(fabs(mTempCharPos.x - mCharPos.x) > 1.0f &&
		fabs(mTempCharPos.y - mCharPos.y) < 1.0f)
	{
		mTempCharPos.x += mVelocity.x*deltaTime;
	}
	else if(fabs(mTempCharPos.x - mCharPos.x) < 1.0f &&
		fabs(mTempCharPos.y - mCharPos.y) > 1.0f)
	{
		mTempCharPos.y += mVelocity.y*deltaTime;
	}
	else 
	{
		mTempCharPos += mVelocity*deltaTime;
	}
}
void Enemy::Render(const SVector2& viewOffset)
{
	if (mHealth > 0)
	{
		if(mIsMoving == true)
		{
			
			SVector2 temp(mTempCharPos + viewOffset);
			if(IsLeft())
			{
				mMovementRightLeft.SetPosition(temp);
				mMovementRightLeft.Render();
			}
			else
			{
				mMovementLeftRight.SetPosition(temp);
				mMovementLeftRight.Render();
			}
		}
		else
		{
			UpdateState();
			for(int i = E_topLeft; i < E_dead; ++i)
			{
				mEnemySprite[i].SetPosition(mCharPos+viewOffset);
			}
			mEnemySprite[mCurrentState].Render();
		}
		
	}
}

int Enemy::GetHealth()
{
	return mHealth;
}
int Enemy::GetMovement()
{
	return mMovement;
}
int Enemy::GetAttack() const
{
	return mAttack;
}
int Enemy::GetDefence()
{
	return mMagic;
}
int Enemy::GetMagic()
{
	return mMagic;
}
int Enemy::SetHealth(int num)
{
	mHealth = num;
	return mHealth;
}
int Enemy::SetMovement(int num)
{
	mMovement = num;
	return mMovement;
}
int Enemy::SetAttack(int num)
{
	mAttack = num;
	return mAttack;
}
int Enemy::SetDefence(int num)
{
	mDefence = num;
	return mDefence;
}
int Enemy::SetMagic(int num)
{
	mMagic = num;
	return mMagic;
}
SVector2 Enemy::Action(const IsometricMap map)
{
	SVector2 temp(map.GetScreenCoordinateChar());
	SetEnemyPos(map.GetScreenPositionPixelChar(temp));
	return map.GetScreenPositionPixelChar(temp);
}
void Enemy::Defend(const Character& attackers)
{
	mHealth -= (attackers.GetAttack() - mDefence);
	if(mHealth < 0)
	{
		mHealth = 0;
	}
}
void Enemy::UpdateState()
{
	SVector2 middleDivider(IniFile_GetInt("RowNumber", 8)/2 ,IniFile_GetInt("ColumnNumber", 8)/2);
	if(mCharPos.x  <= middleDivider.x)
	{
		if(mCharPos.y <= middleDivider.y)
		{
			mCurrentState = E_topLeft;
		}
		else
		{
			mCurrentState = E_bottomLeft;
		}
	}
	else 
	{
		if(mCharPos.y <= middleDivider.y)
		{
			mCurrentState = E_topRight;
		}
		else
		{
			mCurrentState = E_bottomRight;
		}
	}
}
bool Enemy::IsLeft() const
{
	if(mCharPos.x <= mTempCharPos.x)
	{
		return true;
	}
	else
	{
		return false;
	}
}
